package com.yobol.jgrowth.controller;

import com.yobol.jgrowth.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Page Controller
 *
 * @author yobol
 * @create 2018-03-24 19:26
 **/

@Controller
public class PageController {

     @Autowired
     private ArticleService articleService;

     @RequestMapping(value = {"admin","/admin"})
     public String admin(HttpServletRequest request, HttpServletResponse response) {
         Integer rid = (Integer)request.getSession().getAttribute("login_status");
         if (rid == null || rid <= 1){
             return index(request);
         } else {
             return "admin";
         }
     }

    @RequestMapping(value = {"/","/index"})
    public String index(HttpServletRequest request){
        request.getSession().setAttribute("javabasic",articleService.getArticlesByTagRank("Java语言基础",3));

        request.getSession().setAttribute("web",articleService.getArticlesByTagRank("Web",3));
        request.getSession().setAttribute("designpattern",articleService.getArticlesByTagRank("设计模式",3));
        request.getSession().setAttribute("banners",articleService.getArticlesOfBanner(3));
        return "index";
    }

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/register")
    public String register(){
        return "register";
    }

    @RequestMapping(value = "/coding")
    public String coding(HttpServletRequest request){
         int codingCounter = (request.getSession().getAttribute("codingCounter") == null) ? 1 : (Integer)request.getSession().getAttribute("codingCounter");
         if (codingCounter <= 1){
             request.getSession().setAttribute("code","public class Friend{\n" +
                     "\tprivate String name;\n" +
                     "  \tprivate int age;\n" +
                     "  \n" +
                     "  \tpublic Friend(String name,int age){\n" +
                     "    \tthis.name = name;\n" +
                     "      \tthis.age = age;\n" +
                     "    }\n" +
                     "  \n" +
                     "  \tpublic String getName(){\n" +
                     "    \treturn this.name;\n" +
                     "    }\n" +
                     "  \n" +
                     "  \tpublic int getAge(){\n" +
                     "    \treturn this.age;\n" +
                     "    }\n" +
                     "  \t\n" +
                     "  \tpublic static void main(String[] args){\n" +
                     "      \tFriend friend = new Friend(\"Yobol\",22);\n" +
                     "    \tSystem.out.println(\"Hello,\" + friend.getName());\n" +
                     "    }\n" +
                     "}");
         }
         codingCounter++;
         request.getSession().setAttribute("codingCounter",codingCounter);
         return "coding";
    }

    @RequestMapping(value = "/architect")
    public String architect(){
        return "architect";
    }

    @RequestMapping(value = "/articles")
    public String articles(){
        return "articles";
    }

    @RequestMapping(value = "/article")
    public String article(){
        return "article";
    }

    @RequestMapping(value = "/editor")
    public String editor(){
        return "editor";
    }
}
